<?php

namespace app\services\activity\free;

use app\dao\activity\free\StoreFreeRulesDao;
use app\model\activity\free\FreeRuleProduct;
use app\model\activity\free\StoreFreeRule;
use app\services\BaseServices;

class StoreFreeRulesServices extends BaseServices
{
    public function __construct(StoreFreeRulesDao $dao)
    {
        $this->dao = $dao;
    }


    public function syncFreeRules($free_id, $rules)
    {
        if (empty($rules)) {
            StoreFreeRule::where('free_id', $free_id)->update([
                'is_del'    =>  0
            ]);
            return true;
        }

        try {
            // 开启事务
            StoreFreeRule::startTrans();


            foreach ($rules as $rule) {
                // 判断存不存在id
                $rule_id = (isset($rule['id']) && $rule['id']) ? $rule['id'] : 0;
                $rule_sql = null;

                $min_price = ($rule['type'] == 2) ? 0 : $rule['min_price'];
                $min_count = ($rule['type'] == 1) ? 0 : $rule['min_count'];

                if ($rule_id) {
                    $rule_sql = StoreFreeRule::where('id', $rule_id)->find();
                    if (!$rule_sql) {
                        continue;
                    }
                    $rule_sql->update([
                            'id' => $rule_sql->id,
                            'free_id' => $free_id,
                            'min_price' => $min_price,
                            'nums' => $rule['nums'],
                            'min_count' => $min_count
                        ]
                    );
                } else {
                    $res = StoreFreeRule::create([
                        'free_id' => $free_id,
                        'min_price' => $min_price,
                        'nums' => $rule['nums'],
                        'min_count' => $min_count,
                        'add_time' => time()
                    ]);

                    $rule_id = $res->id ?? 0;
                }
                if ($rule_id) {
                    FreeRuleProduct::where('free_rule_id', $rule_id)->delete();
                    foreach ($rule['products'] as $product) {
                        FreeRuleProduct::create([
                            'free_rule_id' => $rule_id,
                            'product_id' => $product['product_id'],
                            'give_count' => $product['give_count'],
                            'activity_count' => $product['activity_count'],
                            'product_attr_value_unique' => $product['product_attr_value_unique'],
                            'add_time' => time()
                        ]);
                    }
                }
            }

            // 提交事务
            StoreFreeRule::commit();

            return true; // 执行成功，返回 true

        } catch (\Exception $e) {

            // 回滚事务
            StoreFreeRule::rollback();

            // 捕获异常，返回 false
            return false;
        }
    }


    public function syncFreeRulesOld($free_id, $rules)
    {
        try {
            // 开启事务
            StoreFreeRule::startTrans();

            // 删除原有规则和规则商品
            $rule_sql = StoreFreeRule::where('free_id', $free_id)->find();
            FreeRuleProduct::where('free_rule_id', $free_id)->delete();


            // 插入新的规则和规则商品
            foreach ($rules as $rule) {
                $min_price = ($rule['type'] == 2) ? 0 : $rule['min_price'];
                $min_count = ($rule['type'] == 1) ? 0 : $rule['min_count'];

                if ($rule_sql) {
                    $rule_sql->update([
                        'id'    => $rule_sql->id,
                        'free_id' => $free_id,
                        'min_price' => $min_price,
                        'nums' => $rule['nums'],
                        'min_count' => $min_count,
                    ]);
                } else {

                    $rule_sql = StoreFreeRule::create([
                        'free_id' => $free_id,
                        'min_price' => $min_price,
                        'nums' => $rule['nums'],
                        'min_count' => $min_count,
                        'add_time'  =>  time()
                    ]);

                }

                foreach ($rule['products'] as $product) {
                    FreeRuleProduct::create([
                        'free_rule_id' => $rule_sql->id,
                        'product_id' => $product['product_id'],
                        'product_attr_value_unique'   =>  $product['product_attr_value_unique'],
                        'give_count' => $product['give_count'],
                        'activity_count' => $product['activity_count'],
                        'add_time' => time(), // 假设 add_time 是当前时间戳
                    ]);
                }
            }

            // 提交事务
            StoreFreeRule::commit();

            return true; // 执行成功，返回 true
        } catch (\Exception $e) {
            // 回滚事务
            StoreFreeRule::rollback();

            // 捕获异常，返回 false
            return false;
        }
    }

}